<template>
  <div class="index">
    <p style="text-align: center;font-size: 18px;color: #333333;font-weight: 600">{{$route.query.questionName}}</p>
    <p style="text-align: right;font-size: 14px;color: #333333;">截止时间 : {{ $Time($route.query.time).getTime()}}</p>
    <el-row  :ref="'question-'+index" v-for="(item,index) in questionList" :key="index" style="margin-bottom: 50px">
      <el-row>
        {{index+1}}、【{{item.testType}} | {{item.fraction }}分】
        <el-button @click="questionList.splice(index,1)" type="text"><i class="el-icon-delete"></i></el-button>
      </el-row>
      <singleChoice :ref="'question-info-'+index" :index="index+1" :item="item" v-if="item.testType === '单选题'" />
      <singleChoice :ref="'question-info-'+index" :index="index+1" :item="item" v-else-if="item.testType === '简答题'" />
      <multipleChoice :ref="'question-info-'+index" :index="index+1" :item="item" v-else-if="item.testType === '多选题'" />
      <judgeChoice :ref="'question-info-'+index" :index="index+1" :item="item" v-else-if="item.testType === '判断题'" />
      <nullChoice :ref="'question-info-'+index" :index="index+1" :item="item" v-else-if="item.testType === '填空题'" />
    </el-row>
    <el-row style="background-color: #ffffff;position: fixed;bottom: 0;width:calc( 100% - 230px);padding: 10px 50px">
      <el-button style="float: right" type="primary" @click="submit">提交</el-button>
    </el-row>
  </div>
</template>

<script>
  import singleChoice from './singleChoice'
  import multipleChoice from './multipleChoice'
  import judgeChoice from './judgeChoice'
  import nullChoice from './nullChoice'
  export default {
    name: "index",
    components: { singleChoice,multipleChoice,judgeChoice,nullChoice},
    props: [''],
    data() {
      return {
        questionList: [],
        testTypeList:[]
      }
    },
    provide: {},
    inject: [],
    computed: {},
    methods: {
      submit(){
        new Promise((rs,rj)=>{
          let fraction = 0
          this.questionList.map((item,index)=>{
            let question_info = this.$refs['question-info-'+index][0]
            if(!question_info.isSuccess){
              rj(index+1)
            }
            fraction += question_info.answer ? Number(item.fraction) : 0
          })
          rs(fraction)
        }).then((fraction)=>{
          let cnt = {
            testId:this.$route.query.ids,
            fraction,
            ids:Date.now(),
            userId:this.$adminInfo().ids,
            time:Date.now(),
          }
          this.$api.add('test_info',cnt,res=>{
            this.$notify({
              title: '成功',
              message: '答题成功，您的分数是：'+fraction,
              type: 'success',
              duration: 0
            })

            this.$routerGo('/admin-test-info')
          })

        }).catch((index)=>{
          this.$message(`请填写完整第${index}题`)
        })
      },
    },
    mounted() {
      this.$api.get('testType',{$orderBY: {col:'ids'}},res=>{
        this.testTypeList=res
      })
      let query = this.$route.query
      let cnt ={
        $where :{ questionId : query.questionId },
        $orderBY: {
          col: 'indexs',
          methods:'ASC'
        }
      }
      this.$api.get('question_info',cnt,res=>{
        if(res){
          res.map(item=>{
            item.response = this.$JSP(item.answer)
            if(item.testType === '填空题'){
              item.question = item.name
              item.answer = ''
            }else if( item.testType === '单选题' ){
              let name = JSON.parse(decodeURIComponent(atob(item.name)))
              item.A = name.A
              item.B = name.B
              item.C = name.C
              item.D = name.D
              item.answer = ''
              item.question = name.question
            }else if( item.testType === '多选题' ){
              let name = JSON.parse(decodeURIComponent(atob(item.name)))
              item.A = name.A
              item.B = name.B
              item.C = name.C
              item.D = name.D
              item.question = name.question
              item.response = item.answer.split(',')
              item.answer = []
            }else if( item.testType === '判断题' ){
              item.question = item.name
              item.answer = ''
            }
            item.fraction = this.testTypeList.filter(it => it.name === item.testType)[0].fraction
          })
          this.questionList = res
        }
      })
    },
    watch: {},
  }
</script>

<style scoped lang='scss'>
  .index {

  }
</style>
